package com.letou.ls.uitl;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;


public class UiSizeHelper {
    private static final String TAG = "UiSize";

    private static final int BASE_WIDTH = 720;
    private static final int BASE_HEIGHT = 1080;

    private static int sScreenWidth = 0;
    private static int sScreenHeight = 0;
    private static float sScreenScale = 0.0f;
    private static Resources sResources = null;
    private static int sOrientation = 0;

    private static float hScale;

    public static void init(Context context) {
        sResources = context.getApplicationContext().getResources();
        DisplayMetrics dm = sResources.getDisplayMetrics();
        sScreenWidth = dm.widthPixels;
        sScreenHeight = dm.heightPixels;
        sOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        float wScale = (float) sScreenWidth / BASE_WIDTH;
        hScale = (float) sScreenHeight / BASE_HEIGHT;
        sScreenScale = (wScale > hScale) ? hScale : wScale;
        Log.e("UiSize", "hScale:" + sScreenWidth + ";" + sScreenHeight + ";" + sScreenScale);
    }

    public static float getDimension(int id) {
        return sScreenScale * sResources.getDimension(id);
    }

    public static int getIntDimension(int id) {
        return (int) (sScreenScale * sResources.getDimension(id));
    }

    public static int getOrientation() {
        return sOrientation;
    }

    public static int getScreenWidth() {
        return sScreenWidth;
    }

    public static int getScreenHeight() {
        return sScreenHeight;
    }

    public static float scalePx(Context context, int arrowHeightDialer) {
        return sScreenScale * arrowHeightDialer;
    }

    public static float scaleHeightPx(Context context, int arrowHeightDialer) {
        return hScale * arrowHeightDialer;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
